// express-validator 验证中间件模块

import { NextFunction, Request, Response } from 'express';
import { ValidationChain, validationResult } from 'express-validator';

const validate = (validations: ValidationChain[]) => {
    return async (req: Request, res: Response, next: NextFunction) => {
        await Promise.all(validations.map(validation => validation.run(req)));

        const errors = validationResult(req);
        if (errors.isEmpty()) {
            return next();
        }

        let msgArr = errors.array().map(item => item.msg);
        res.sendError(JSON.stringify(msgArr.join(' | ')), 400);
    };
};

export default validate;
